package com.zpz.framework.zpzoauth.common.result;

import com.alibaba.fastjson.JSON;
import com.zpz.framework.zpzoauth.common.encrypt.DefaultCrypto;
import com.zpz.framework.zpzoauth.common.encrypt.SignUtil;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthCommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangpanzhi
 * @description
 * @create 2020-03-20 13:19
 */
public class GatewayResult {
    private static Logger log = LoggerFactory.getLogger(GatewayResult.class);
    public static Map<String,Object> getResponseBody(String apicode, Enum<?> status, String data, String error, String en, String sign){
        ZpzOauthResultMsg<Object> re=null;
        boolean decrypt = en.equals("1") ? true : false;
        boolean signFlag = sign.equals("1") ? true : false;
        String signStr=null;
        String nonceStr=null;
        if (decrypt){
            try {
                data= URLEncoder.encode(data,"UTF-8");
                data = DefaultCrypto.encodDataPublicKey(data, ZpzOauthCommonConstant.PUBLIC_KEY);
                log.info("业务请求["+apicode+"]：:：：:::::::::::加密数据：：：：："+data);
//                String s = DefaultCrypto.decodDataPrivateKey(data, CommonConstant.PRIVATE_KEY);
//                log.info(":::::::::::::解密数据：：：：："+s);
                if (signFlag){
                    try {
                        nonceStr = SignUtil.generateNonceStr();//随机字符串
                        signStr = DefaultCrypto.sign(data, nonceStr, ZpzOauthCommonConstant.SIGN_SECRET_KEY);
                    } catch (Exception e) {
                        error=e.getMessage();
                        re= ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10021);
                        decrypt = false;
                        signFlag = false;
                    }
                }
            } catch (Exception e) {
                error=e.getMessage();
                re= ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10019);
                decrypt = false;
                signFlag = false;
            }

        }
        if (re==null){
            re= ZpzOauthResultMsg.customResultStatus(status);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",re.getCode());
        map.put("decrypt_flag",decrypt);
        map.put("sign_flag",signFlag);
        if (StringUtils.isNotBlank(data)){
            try {
                map.put("data",StringUtils.isNotBlank(data)? JSON.parse(data):"");
            } catch (Exception e) {
                map.put("data",StringUtils.isNotBlank(data)?data:"");
            }
        }else{
            map.put("data",StringUtils.isNotBlank(data)?data:"");
        }
        map.put("sign",StringUtils.isNotBlank(signStr)?signStr:"");
        map.put("nonceStr",StringUtils.isNotBlank(nonceStr)?nonceStr:"");
        map.put("error",re.getCode()!=0?(StringUtils.isNotBlank(re.getMessage())?re.getMessage():""):"");
        map.put("errorDetail",StringUtils.isNotBlank(error)?error:"");
        return map;
    }
}
